﻿using B.S.BasicData.API.Read.Application.Command;
using B.S.BasicData.API.Read.Application.Command.User;
using B.S.BasicData.API.Read.DTOS;
using B.S.BasicData.API.Read.DTOS.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.S.BasicData.API.Read.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IMediator mediator;

        public UserController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public Task<ApiResult<List<UserDTO>>> GetUser([FromQuery]GetUserCommand request)
        {
            try
            {
                var Id = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var Name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                var Role = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
                var Key = HttpContext.User.FindFirst("key")?.Value;
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据部门Id获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public Task<ApiResult<List<UserDTO>>> GetUserByDepartmentId([FromQuery] GetUserByDepartmentIdCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<LoginDTO>> Login([FromQuery]LoginUserCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
